package sdstcom.lbq.one;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

import my_a10bbc.ContentValue;
import my_a10bbc.cn.xp.com.bean.IEveryDayBean;
import my_a10bbc.cn.xp.com.fragment.IindexFragment;
import sdstcom.lbq.one.lbq.Fragment.Essayfragment;
import sdstcom.lbq.one.lbq.Fragment.Musicfragment;
import sdstcom.lbq.one.lbq.Fragment.Videofragment;

public class MainActivity extends AppCompatActivity {
  private DrawerLayout drawerLayout;
    private LinearLayout ll;
    private ArrayList<Fragment> list;
    private RadioGroup group;
    private int state=0;
    private FragmentManager manager;
    private int count;
    private ArrayList<String> list_String;
    private CallBack callBack;


    private FragmentTransaction transition;
    @Override
    protected void onCreate(final Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        init();
        IindexFragment index=IindexFragment.getInstance("abc");
        this.callBack=index;
        list.add(index);
        list.add(Essayfragment.Initfragment());
        list.add(Musicfragment.initfragment());
        list.add(Videofragment.Initfragment());
        transition.add(R.id.ll,list.get(0));
        transition.add(R.id.ll,list.get(1));
        transition.add(R.id.ll,list.get(2));
        transition.add(R.id.ll,list.get(3));
        transition.hide(list.get(1));
        transition.hide(list.get(2));
        transition.hide(list.get(3));
        transition.commit();
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId)
                {
                   case  R.id.main_first:
                      manager.beginTransaction().hide(list.get(state)).show(list.get(0)).commit();
                       state=0;
                    break;
                    case  R.id.main_essay:
                        manager.beginTransaction().hide(list.get(state)).show(list.get(1)).commit();

                        state=1;
                        break;
                    case  R.id.main_music:
                        manager.beginTransaction().hide(list.get(state)).show(list.get(2)).commit();
                        state=2;
                        break;
                    case  R.id.main_video:
                        manager.beginTransaction().hide(list.get(state)).show(list.get(3)).commit();
                        state=3;
                        break;


                }
            }
        });
    drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {



        }

        @Override
        public void onDrawerOpened(View drawerView) {
            if (Build.VERSION.SDK_INT>21)
            {  startActivity(new Intent(MainActivity.this,RightScrollActivity.class), ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle());}
          else
            {
                startActivity(new Intent(MainActivity.this,RightScrollActivity.class));
            }


            drawerLayout.closeDrawer(Gravity.LEFT);


        }

        @Override
        public void onDrawerClosed(View drawerView) {


        }
        @Override
        public void onDrawerStateChanged(int newState) {

        }
    });

    }



    public void init() {
        list_String=new ArrayList<>();
        ll = (LinearLayout) findViewById(R.id.ll);
        group = (RadioGroup) findViewById(R.id.main_rg);
        manager = getSupportFragmentManager();
        transition = manager.beginTransaction();
        list = new ArrayList<>();
        drawerLayout= (DrawerLayout)  findViewById(R.id.activity_main);

        RequestParams param=new RequestParams(ContentValue.URL_EVERYDAY);
        x.http().get(param, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                IEveryDayBean obj=new Gson().fromJson(result,IEveryDayBean.class);
                list_String.addAll(obj.data);
                Log.e("TS", "onSuccess"+list_String.get(0) );
                callBack.SendList(list_String);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });





    }

    /***
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
       callBack.setSelection();
    }

    @Override
    protected void onResume() {
        super.onResume();
        count=0;
    }

    @Override
    protected void onPause() {
        super.onPause();
        count=0;
    }
    public interface CallBack{
        void SendList(ArrayList<String> l);
        void setSelection();
    }
}
